#!/bin/sh

echo "Building SimGear for win32"

SRC_ROOT=~curt/Projects/SimGear-0.3/SimGear
BUILD_ROOT=/tmp/simgear-win32

# find the source file
FILE=`\ls -t $SRC_ROOT/SimGear-*.tar.gz | head -1`
echo "Source file = $FILE"

# remove any possible remnants of a previous build
\rm -rf $BUILD_ROOT

# make the build area
mkdir -p $BUILD_ROOT

# unroll the latest snap shot in the build area
cd $BUILD_ROOT && tar xzvf $FILE

# cd to the newly created directory
DIR=`basename $FILE .tar.gz`
echo need to cd to $BUILD_ROOT/$DIR
cd $BUILD_ROOT/$DIR || echo "cannot cd to source root"

# setup path for cross compiler
export PATH=/usr/local/cygwin-b20/bin:$PATH

# configure the project
echo CC=i386-cygwin32-gcc CXX=i386-cygwin32-g++ CPPFLAGS=-I/usr/local/cygwin-b20/include LDFLAGS=-L/usr/local/cygwin-b20/lib ./configure --prefix=/usr/local/cygwin-b20 --host=i686-pc-linux-gnu
CC=i386-cygwin32-gcc CXX=i386-cygwin32-g++ CPPFLAGS=-I/usr/local/cygwin-b20/include LDFLAGS=-L/usr/local/cygwin-b20/lib ./configure --prefix=/usr/local/cygwin-b20 --host=i686-pc-linux-gnu

# build the project
make

# final word
echo "now make install to get the new headers installed in the system location"
echo "then redo the whole process from scratch ..."
echo "as root:"
echo ""
echo "export PATH=/usr/local/cygwin-b20/bin:\$PATH"
echo "cd $BUILD_ROOT/$DIR"
echo "make install"
